/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.portalnahouby.implementation.serviceimplementation;

import cz.muni.fi.pa165.portalnahouby.api.dto.Edibility;
import cz.muni.fi.pa165.portalnahouby.api.dto.MushroomDTO;
import cz.muni.fi.pa165.portalnahouby.api.serviceinterface.MushroomService;
import cz.muni.fi.pa165.portalnahouby.implementation.daointerface.MushroomDAO;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Mushroom;
import cz.muni.fi.pa165.portalnahouby.implementation.util.EntityDTOConvertor;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  Implementation of mushroom service
 * @author Jakub Kutil
 */
@Service
public class MushroomServiceImpl implements MushroomService {

    @Autowired
    private MushroomDAO mushroomDAO;

    public void setMushroomDAO(MushroomDAO mushroomDAO) {
        this.mushroomDAO = mushroomDAO;
    }

    @Override
    public void addMushroom(MushroomDTO mushroom) {
        Mushroom mushroomEntity = EntityDTOConvertor.convertMushroom(mushroom);
		mushroomDAO.addMushroom( mushroomEntity);
		mushroom.setId(mushroomEntity.getId());
    }

    @Override
    public void editMushroom(MushroomDTO mushroom) {
        mushroomDAO.editMushroom(EntityDTOConvertor.convertMushroom(mushroom));
    }

    @Override
    public void deleteMushroom(Long id) {
        mushroomDAO.deleteMushroom(id);
    }

    @Override
    public MushroomDTO findMushroom(Long id) {
        return EntityDTOConvertor.convertMushroom(mushroomDAO.findMushroom(id));
    }

    @Override
    public List<MushroomDTO> getListSortByName() {
        List<Mushroom> entityList = mushroomDAO.getListSortByName();
        List<MushroomDTO> dtoList = new ArrayList<MushroomDTO>();

        for (Mushroom mushroom : entityList) {
            dtoList.add(EntityDTOConvertor.convertMushroom(mushroom));
        }

        return dtoList;
    }

    @Override
    public List<MushroomDTO> getListByEdibility(Edibility edibility) {
        List<Mushroom> entityList = mushroomDAO.getListByEdibility(edibility);
        List<MushroomDTO> dtoList = new ArrayList<MushroomDTO>();

        for (Mushroom mushroom : entityList) {
            dtoList.add(EntityDTOConvertor.convertMushroom(mushroom));
        }

        return dtoList;
    }
}
